package com.yonyou.pmclouds.siteinspection.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.*;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.inspectitem.rmiitf.InspectItemService;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.project.processor.ProjectBillUpdateForUpdate;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForDeleteProcessor;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.siteinspection.entity.SiteInspectionVO;
import com.yonyou.pmclouds.siteinspection.mapper.SiteInspectionMapper;
import com.yonyou.pmclouds.siteinspection.process.CountPassRateProcessor;
import com.yonyou.pmclouds.siteinspection.process.SiteInspectionBeforeAddProcess;
import com.yonyou.pmclouds.siteinspection.process.SiteInspectionInspectItemProcess;
import com.yonyou.pmclouds.siteinspection.process.SiteInspectionProjectLogPostProcess;
import com.yonyou.pmclouds.siteinspection.rmiitf.PatrolMaintain;
import com.yonyou.pmclouds.siteinspection.rmiitf.SiteInspectionQuery;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = PatrolMaintain.class)
public class PatrolMaintainImpl implements PatrolMaintain, IFlowApproveBuziService {

    @Autowired
    private SiteInspectionMapper mapper;
    @Autowired
    private PictureRemoteService pictureService;
    @Autowired
    private MobileTaskInsertProcessor<SiteInspectionVO> mobileTaskInsertProcessor;
    @Autowired
    private SiteInspectionInspectItemProcess inspectItemProcess;
    @Autowired
    private ProjectStatusCheckProcessor<SiteInspectionVO> projectStatusCheckProcessor;
    @Autowired
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<SiteInspectionVO> billCountFactory;
    @Autowired
    private SiteInspectionProjectLogPostProcess siteInspectionProjectLogPostProcess;
    @Autowired
    private CountPassRateProcessor countPassRateProcessor;
    @Autowired
    private DocCenterProcessor docCenterProcessor;
    @Autowired
    private UpdateBillCountForDeleteProcessor.UpdateBillCountForDeleteProcessorFactory<SiteInspectionVO> billCountDeleteFactory;
    @Autowired
    private SiteInspectionQuery siteInspectionQuery;
    @Autowired
    private ProjectBillUpdateForUpdate.ProjectBillForUpdateProcessorFactory<SiteInspectionVO> billForUpdateProcessorFactory;
    @Autowired
    private InspectItemService inspectItemService;
    //更新状态字段
    private static String[] UPDATE_STATUS = {SiteInspectionVO.BILLSTATUS};

    //主表需要更新的字段名(施工单位，工程部位，检查标准)
    private static String[] UPDATE_FIELDS = {SiteInspectionVO.PK_CONSTRUCTIONUNIT, CommonFieldConst.PKPROJECTPART,SiteInspectionVO.PK_INSPECTSTANDARD};

    @PMApprove
    @Override
    public SiteInspectionVO add(SiteInspectionVO vo) throws BusinessException {
        SingleInsertBpTemplate<SiteInspectionVO> bp = new SingleInsertBpTemplate<SiteInspectionVO>(mapper);
        bp.addBeforeProcessor(new SiteInspectionBeforeAddProcess());
        // 更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<SiteInspectionVO>());
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        bp.addBeforeProcessor(countPassRateProcessor);
        // 更新单据数量统计
        bp.addAfterProcessor(billCountFactory.getProcessor(BillTypeConst.PATROL_BILLTYPE));
        // 现场检查新增完之后新增表体检查项
        bp.addAfterProcessor(inspectItemProcess);
        bp.addAfterProcessor(billCountFactory.getProcessor(vo.getBill_type()));
        // 智能日志
        bp.addAfterProcessor(siteInspectionProjectLogPostProcess);
        // NCC文档中心
        bp.addAfterProcessor(docCenterProcessor);
        bp.insert(vo);
        // 新增后上传图片
        pictureService.insertPictureForPub(vo, null);
        return vo;
    }

    @PMApprove(trigger = "commit")
    @Override
    public SiteInspectionVO update(SiteInspectionVO vo) throws BusinessException {
        //创建一个单个实体更新模板对象，传入你要更新的实体的mapper对象以及你想要更新或者说可以更新的字段名。这样在更新时会自动只更新这些字段。
        SingleUpdateBpTemplate<SiteInspectionVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
        //添加修改者以及修改时间
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<SiteInspectionVO>());
        //检验项目状态，判断项目是否已经竣工
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        //更新统计单据，主要更新项目单据统计单据状态
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.PATROL_BILLTYPE));
        //更新检查项的检查记录内容
        inspectItemService.update(vo.getInspectitems(),vo);
        //存储图片和小视频
        pictureService.insertPictureForPub(vo, null);
        //更新主表数据
        bp.update(vo);
        return vo;
    }

    @Override
    public SuperVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException {
        //更新统计单据，主要更新项目单据统计单据状态
        SiteInspectionVO siteInspectionVO = mapper.getById(pk_bill);
        SingleUpdateBpTemplate<SiteInspectionVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);
        siteInspectionVO.setBillstatus(BillStatusConst.COMMON_APPROVED_STATUS);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.PATROL_BILLTYPE));
        bp.update(siteInspectionVO);
        return siteInspectionQuery.queryOne(pk_bill);
    }

    @Override
    public SuperVO rejectByPkBill(String pk_bill) throws BusinessException {
        //更新统计单据，主要更新项目单据统计单据状态
        billForUpdateProcessorFactory.getProcessor(BillTypeConst.PATROL_BILLTYPE);
        SiteInspectionVO siteInspectionVO = mapper.getById(pk_bill);
        SingleUpdateBpTemplate<SiteInspectionVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);
        siteInspectionVO.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.PATROL_BILLTYPE));
        bp.update(siteInspectionVO);
        return siteInspectionQuery.queryOne(pk_bill);
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        return siteInspectionQuery.queryOne(pk_bill);
    }

    @Override
    public String getBillType() {
        return BillTypeConst.PATROL_BILLTYPE;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<SiteInspectionVO> bp = new SingleDeleteBpTemplate<>(mapper);
        bp.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.PATROL_BILLTYPE));
        return bp.delete((SiteInspectionVO) businessVO);
    }

    @Override
    public Object getMapper() {
        return mapper;
    }
}
